﻿using Mapster;
using Minotaur.App.Core.Contrls;
using Minotaur.App.Domains.Enum;
using Minotaur.App.Domains.ViewModel;
using Prism.Commands;
using Prism.Mvvm;
using ServiceStack;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace Minotaur.App.Modules.SystemControl.ViewModels
{
    public class PaginationViewModel : BindableBase
    {
        public PaginationViewModel()
        {

        }
        private int _Total;
        private int _PageSize = 20;
        private int _PageNumber = 1;
        private bool _IsFirstPage = false;
        private bool _IsLastPage = false;
        private int _PageCount = 0;
        public PaginationType PaginationType;
        private List<int> _PageSizeConfig = new List<int>()
        {
            10,
            20,
            40,
            60,
            80,
            100,
            200,
            400,
        };
        private ObservableCollection<PageNumberClass> _PageList = new ObservableCollection<PageNumberClass>();

        /// <summary>
        /// 用户控件
        /// </summary>
        public UserControl UserControl { get; set; }

        /// <summary>
        /// 总计条数
        /// </summary>
        public int Total
        {
            get => _Total; set
            {
                SetProperty(ref _Total, value);
                InitPaginations();
            }
        }

        /// <summary>
        /// 每页条数
        /// </summary>
        public int PageSize { get => _PageSize; set => SetProperty(ref _PageSize, value); }

        /// <summary>
        /// 当前也索引
        /// </summary>
        public int PageNumber { get => _PageNumber; set => SetProperty(ref _PageNumber, value); }

        /// <summary>
        /// 每页条数配置
        /// </summary>
        public List<int> PageSizeConfig { get => _PageSizeConfig; set => SetProperty(ref _PageSizeConfig, value); }



        /// <summary>
        /// 条数据
        /// </summary>
        public ObservableCollection<PageNumberClass> PageList { get => _PageList; set => SetProperty(ref _PageList, value); }
        public bool IsFirstPage { get => _IsFirstPage; set => SetProperty(ref _IsFirstPage, value); }
        public bool IsLastPage { get => _IsLastPage; set => SetProperty(ref _IsLastPage, value); }
        /// <summary>
        /// 总共多少页
        /// </summary>
        public int PageCount { get => _PageCount; set => SetProperty(ref _PageCount, value); }

        #region 函数注册区域

        private void InitPaginations()
        {
            PageCount = (Total + PageSize - 1) / PageSize; //推荐写法 
            int length = PageCount;

            List<PageNumberClass> pages = new List<PageNumberClass>();
            #region 启用/禁用上一页/下一页按钮
            if (PageCount > 1)
            {
                IsLastPage = true;
                if (PageNumber > 1)
                    this.IsFirstPage = true;
                else this.IsFirstPage = false;
                if (this.PageNumber == PageCount) this.IsLastPage = false;
            }
            if (Total <= this.PageSize)
            {
                this.IsLastPage = false;
                this.IsFirstPage = false;
            }
            #endregion
            for (int i = 1; i <= this.PageCount; i++)
            {
                var page = new PageNumberClass()
                {
                    Id = i
                };
                page.CurrentPageAction = CurrentPageAction;
                if (i == PageNumber)
                {
                    page.Foreground = new SolidColorBrush(Colors.BlueViolet);
                    page.BorderBrush = new SolidColorBrush(Colors.BlueViolet);
                }
                pages.Add(page);
            }
            if (this.PageCount > 10)
            {

                ///当前页码小于10时
                if (this.PageNumber <= 10)
                {
                    InitPageModel(0, pages);
                }
                else
                {
                    int id = this.PageList.Min(o => o.Id);
                    switch (this.PaginationType)
                    {
                        //上一步
                        case PaginationType.Frist:
                            {
                                //是否要跳转到上一组
                                if (this.PageNumber + 1 - this.PageList.Min(o => o.Id) == 0)
                                {
                                    InitPageModel(id - 11, pages);
                                }
                                else
                                {
                                    InitPageModel(id - 1, pages);
                                }
                            }
                            break;
                        //当前页码
                        case PaginationType.Current:
                            {
                                InitPageModel(id - 1, pages);
                            }
                            break;
                        //下一页
                        case PaginationType.Last:
                            {
                                //是否要跳转到下一组
                                if ((this.PageList.Max(o => o.Id) + 1 - this.PageNumber) == 0)
                                {
                                    InitPageModel(this.PageNumber - 1, pages);
                                }
                                else
                                {
                                    InitPageModel(id - 1, pages);
                                }
                            }
                            break;
                    }
                }
            }
            else
            {
                InitPageModel(0, pages);
            }
        }

        private void InitPageModel(int v, List<PageNumberClass> pages)
        {
            pages = pages.Skip(v).Take(10).ToList();
            PageList.Clear();
            PageList = pages.Adapt<ObservableCollection<PageNumberClass>>();
        }


        #endregion

        #region 注册事件命令       

        /// <summary>
        /// 上一步按钮
        /// </summary>
        public ICommand FristPageCommand => new DelegateCommand(FristPageAction);

        private void FristPageAction()
        {
            this.PaginationType = PaginationType.Frist;
            this.PageNumber = this.PageNumber--;
            RoutedEventArgs args = new RoutedEventArgs(PageEvent, new SearchViewModel() { PageIndex = this.PageNumber--, PageSize = PageSize });
            UserControl.RaiseEvent(args);
        }

        /// <summary>
        /// 上一步按钮
        /// </summary>
        public ICommand LastPageCommand => new DelegateCommand(LastPageAction);

        private void LastPageAction()
        {
            this.PaginationType = PaginationType.Last;
            this.PageNumber = this.PageNumber++;
            RoutedEventArgs args = new RoutedEventArgs(PageEvent, new SearchViewModel() { PageIndex = this.PageNumber++, PageSize = PageSize });
            UserControl.RaiseEvent(args);
        }
        private void CurrentPageAction(object PageNumber)
        {
            this.PaginationType = PaginationType.Current;
            this.PageNumber = int.Parse(PageNumber + "");
            RoutedEventArgs args = new RoutedEventArgs(PageEvent, new SearchViewModel() { PageIndex = int.Parse(PageNumber + ""), PageSize = PageSize });
            UserControl.RaiseEvent(args);
        }
        /// <summary>
        /// 选择每页条数事件
        /// </summary>
        public void ConfigPageAction()
        {
            this.PageNumber = 1;
            this.PaginationType = PaginationType.Frist;
            RoutedEventArgs args = new RoutedEventArgs(PageEvent, new SearchViewModel() { PageIndex = 1, PageSize = PageSize });
            UserControl.RaiseEvent(args);
        }

        #region 注册控件事件
        /// <summary>
        /// 注册上一页事件
        /// </summary>
        public static readonly RoutedEvent PageEvent =
            EventManager.RegisterRoutedEvent(ClickNameConst.FristPageEventClick, RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(UserControl));

        /// <summary>
        /// 打开节点属性事件
        /// </summary>
        public event RoutedEventHandler PageEventClick
        {
            add { UserControl.AddHandler(PageEvent, value); }
            remove { UserControl.RemoveHandler(PageEvent, value); }
        }
        #endregion

        #endregion
    }
    public class PageNumberClass : BindableBase
    {
        public int Id { get; set; }
        public SolidColorBrush BackGround { get; set; } = new SolidColorBrush(Colors.Transparent);
        public SolidColorBrush Foreground { get; set; } = new SolidColorBrush(Colors.Snow);
        public SolidColorBrush BorderBrush { get; set; } = new SolidColorBrush(Colors.Snow);
        public Action<object> CurrentPageAction { get; set; }
        public ICommand PageCommand => new DelegateCommand<object>(CurrentPageAction);

    }
}
